package com.unity3d.player;

import java.nio.*;
import java.util.*;
import java.net.*;
import javax.net.ssl.*;
import java.io.*;
import java.nio.channels.*;

class UnityWebRequest implements Runnable
{
    private long a;
    private String b;
    private String c;
    private Map d;

    UnityWebRequest(final long a, final String c, final Map d, final String b) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    private static native void contentLengthCallback(final long p0, final int p1);

    private static native boolean downloadCallback(final long p0, final ByteBuffer p1, final int p2);

    private static native void errorCallback(final long p0, final int p1, final String p2);

    private static native void headerCallback(final long p0, final String p1, final String p2);

    private static native void responseCodeCallback(final long p0, final int p1);

    private static native int uploadCallback(final long p0, final ByteBuffer p1);

    protected void badProtocolCallback(final String s) {
        errorCallback(this.a, 4, s);
    }

    protected void contentLengthCallback(final int n) {
        contentLengthCallback(this.a, n);
    }

    protected boolean downloadCallback(final ByteBuffer byteBuffer, final int n) {
        return downloadCallback(this.a, byteBuffer, n);
    }

    protected void errorCallback(final String s) {
        errorCallback(this.a, 2, s);
    }

    protected void headerCallback(final String s, final String s2) {
        headerCallback(this.a, s, s2);
    }

    protected void headerCallback(final Map map) {
        if (map != null) {
            if (map.size() == 0) {
                return;
            }
            for (final Map.Entry<String, V> entry : map.entrySet()) {
                String s;
                if ((s = entry.getKey()) == null) {
                    s = "Status";
                }
                final Iterator iterator2 = ((List)entry.getValue()).iterator();
                while (iterator2.hasNext()) {
                    this.headerCallback(s, iterator2.next());
                }
            }
        }
    }

    protected void malformattedUrlCallback(final String s) {
        errorCallback(this.a, 5, s);
    }

    protected void responseCodeCallback(final int n) {
        responseCodeCallback(this.a, n);
    }

    @Override
    public void run() {
        try {
            final URL url = new URL(this.b);
            final URLConnection openConnection = url.openConnection();
            if (openConnection instanceof HttpsURLConnection) {
                final SSLSocketFactory a = com.unity3d.player.a.a();
                if (a != null) {
                    ((HttpsURLConnection)openConnection).setSSLSocketFactory(a);
                }
            }
            if (url.getProtocol().equalsIgnoreCase("file") && !url.getHost().isEmpty()) {
                this.malformattedUrlCallback("file:// must use an absolute path");
                return;
            }
            if (openConnection instanceof JarURLConnection) {
                this.badProtocolCallback("A URL Connection to a Java ARchive (JAR) file or an entry in a JAR file is not supported");
                return;
            }
            final boolean b = openConnection instanceof HttpURLConnection;
            if (b) {
                try {
                    final HttpsURLConnection httpsURLConnection = (HttpsURLConnection)openConnection;
                    httpsURLConnection.setRequestMethod(this.c);
                    httpsURLConnection.setInstanceFollowRedirects(false);
                }
                catch (ProtocolException ex) {
                    this.badProtocolCallback(ex.toString());
                    return;
                }
            }
            final Map d = this.d;
            if (d != null) {
                for (final Map.Entry<String, V> entry : d.entrySet()) {
                    openConnection.addRequestProperty(entry.getKey(), (String)entry.getValue());
                }
            }
            final ByteBuffer allocateDirect = ByteBuffer.allocateDirect(131072);
            InputStream errorStream = null;
            if (this.uploadCallback(null) > 0) {
                openConnection.setDoOutput(true);
                try {
                    final OutputStream outputStream = openConnection.getOutputStream();
                    while (true) {
                        final int uploadCallback = this.uploadCallback(allocateDirect);
                        if (uploadCallback <= 0) {
                            break;
                        }
                        outputStream.write(allocateDirect.array(), allocateDirect.arrayOffset(), uploadCallback);
                    }
                }
                catch (Exception ex2) {
                    this.errorCallback(ex2.toString());
                    return;
                }
            }
            if (b) {
                final HttpsURLConnection httpsURLConnection2 = (HttpsURLConnection)openConnection;
                try {
                    this.responseCodeCallback(httpsURLConnection2.getResponseCode());
                }
                catch (IOException ex3) {
                    this.errorCallback(ex3.toString());
                    return;
                }
                catch (UnknownHostException ex4) {
                    this.unknownHostCallback(ex4.toString());
                }
            }
            final Map<String, List<String>> headerFields = openConnection.getHeaderFields();
            this.headerCallback(headerFields);
            if ((headerFields == null || !headerFields.containsKey("content-length")) && openConnection.getContentLength() != -1) {
                this.headerCallback("content-length", String.valueOf(openConnection.getContentLength()));
            }
            if ((headerFields == null || !headerFields.containsKey("content-type")) && openConnection.getContentType() != null) {
                this.headerCallback("content-type", openConnection.getContentType());
            }
            final int contentLength = openConnection.getContentLength();
            if (contentLength > 0) {
                this.contentLengthCallback(contentLength);
            }
            try {
                if (openConnection instanceof HttpURLConnection) {
                    final HttpsURLConnection httpsURLConnection3 = (HttpsURLConnection)openConnection;
                    this.responseCodeCallback(httpsURLConnection3.getResponseCode());
                    errorStream = httpsURLConnection3.getErrorStream();
                }
                InputStream inputStream;
                if ((inputStream = errorStream) == null) {
                    inputStream = openConnection.getInputStream();
                }
                final ReadableByteChannel channel = Channels.newChannel(inputStream);
                while (true) {
                    final int read = channel.read(allocateDirect);
                    if (read == -1 || !this.downloadCallback(allocateDirect, read)) {
                        break;
                    }
                    allocateDirect.clear();
                }
                channel.close();
            }
            catch (Exception ex5) {
                this.errorCallback(ex5.toString());
            }
            catch (SSLHandshakeException ex6) {
                this.sslCannotConnectCallback(ex6.toString());
            }
            catch (UnknownHostException ex7) {
                this.unknownHostCallback(ex7.toString());
            }
        }
        catch (IOException ex8) {
            this.errorCallback(ex8.toString());
        }
        catch (MalformedURLException ex9) {
            this.malformattedUrlCallback(ex9.toString());
        }
    }

    protected void sslCannotConnectCallback(final String s) {
        errorCallback(this.a, 16, s);
    }

    protected void unknownHostCallback(final String s) {
        errorCallback(this.a, 7, s);
    }

    protected int uploadCallback(final ByteBuffer byteBuffer) {
        return uploadCallback(this.a, byteBuffer);
    }
}
